define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'car_dealer_certification/index' + location.search,
                    add_url: 'car_dealer_certification/add',
                    edit_url: 'car_dealer_certification/edit',
                    del_url: 'car_dealer_certification/del',
                    multi_url: 'car_dealer_certification/multi',
                    import_url: 'car_dealer_certification/import',
                    table: 'car_dealer_certification',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,// 默认显示搜索条件
                search: false,// 搜索框
                commonSearch: false,// 搜索按钮
                showToggle: false,// 切换视图
                showColumns: false,// 显示列
                showExport: false,// 导出按钮
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'realname', title: __('Realname'), operate: 'LIKE'},
                        {field: 'province.name', title: __('所在区域'), formatter:function(value,row,index){
                                var html='';
                                html = row.province.name + '/' + row.city.name + '/' + row.area.name;
                                return html;
                            }},
                        {field: 'idnumber', title: __('Idnumber'), operate: 'LIKE'},
                        {field: 'legal_person', title: __('Legal_person'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'mail', title: __('Mail'), operate: 'LIKE'},
                        // {field: 'province_id', title: __('Province_id')},
                        // {field: 'city_id', title: __('City_id')},
                        // {field: 'area_id', title: __('Area_id')},
                        {field: 'idcard_r', title: __('Idcard_r'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'idcard_g', title: __('Idcard_g'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'business_license', title: __('Business_license'), events: Table.api.events.images, formatter: Table.api.formatter.images, operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: {0: __('待审核'), 1: __('已通过'), 2: __('已拒绝')}},
                        // {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'check',
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-check',
                                    url: 'car_dealer_certification/edit',
                                    visible: function (row) {
                                        //重点在这
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status != 1) {
                                            return true;
                                        }

                                    },
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        Toastr.success('操作成功');
                                        $("#table").bootstrapTable('refresh',{});
                                        return false;
                                    }
                                },
                                {
                                    name: 'delete',
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    title: __('删除'),
                                    icon: 'fa fa-trash',
                                    url: 'car_dealer_certification/del',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status != 1){
                                            return true;
                                        }
                                    },
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        Toastr.success('操作成功');
                                        $("#table").bootstrapTable('refresh',{});
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
